package com.taoyi.mhouse.fragments;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.taoyi.mhouse.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tempo on 2015/11/19.
 */
public class HomeFragment extends BaseFragment {
    private View main;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ViewPagerAdapter adapter;
    private Context mContext;

    private OnFragmentInteractionListener mListener;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        main = inflater.inflate(R.layout.framgent_home, container, false);
        mContext = getContext();
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            public void onPageSelected(int position) {
                ((LoadingFreshingFragment) adapter.getItem(position)).onFragmentSelected();
            }
        });
        ((LoadingFreshingFragment) adapter.getItem(0)).onFragmentSelected();
        return main;
    }

    public interface OnFragmentInteractionListener {
        public void setViewPager(ViewPager vp);
    }

    public AppCompatActivity getAppCompatActivity() {
        return (AppCompatActivity) getActivity();
    }


    private void setupViewPager(ViewPager viewPager) {
        /**
         * here  use getFragmentManager will cause some problems ,
         * getChildFragmentManager instead.....
         */
        adapter = new ViewPagerAdapter(getChildFragmentManager());
        adapter.addFragment(new RecommendFragment(), "推荐");
        adapter.addFragment(new ShoppingFragment(), "商城");
        adapter.addFragment(new AnimeFragment(), "动漫");
        adapter.addFragment(new ForumFragment(), "论坛");
        viewPager.setAdapter(adapter);
        mListener.setViewPager(viewPager);
    }

    private class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<LoadingFreshingFragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(LoadingFreshingFragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }

    public View findViewById(int id) {
        return main.findViewById(id);
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public boolean isShowLog() {
        return true;
    }
}
